package com.situation.controller;

import com.github.pagehelper.PageInfo;
import com.situation.annotation.ControllerEndpoint;
import com.situation.pojo.DO.BizHealth;
import com.situation.pojo.VO.HealthVO;
import com.situation.service.BizHealthService;
import com.situation.util.RespUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/health")
public class BizHealthController {

    @Autowired
    private BizHealthService bizHealthService;

    /**
     * 根据用户查询打卡记录
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/history")
    public RespUtil getHistory(int pageNum,int pageSize){

        PageInfo<HealthVO> bizHealthPageInfo = bizHealthService.selectById(pageNum, pageSize);
        if (bizHealthPageInfo!=null){
            return RespUtil.ok().setData(bizHealthPageInfo);
        }else
            return RespUtil.fail();
    }

    /**
     * 判断时候打卡过
     * @param bizHealth
     * @return
     */
    @GetMapping("/isReport")
    public RespUtil isReport(BizHealth bizHealth){
        boolean report = bizHealthService.isReport(bizHealth);
        return RespUtil.ok().setData(report);
    }

    /**
     * 添加健康打卡记录
     * @param bizHealth
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加健康打卡失败",operation = "添加健康打卡")
    @PostMapping("/report")
    @RequiresPermissions({"health:report"})
    public RespUtil addReport(BizHealth bizHealth){
        bizHealth.setCreateTime(LocalDateTime.now());
        int i = bizHealthService.add(bizHealth);
        if (i==1){
            return RespUtil.ok();
        }else
            return RespUtil.fail();
    }
}
